package com.singlemountaintech.haogongjiang.utils;

/**
 * Created by LUOLI on 2016/09/23.
 */

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.singlemountaintech.haogongjiang.R;
import com.singlemountaintech.haogongjiang.qfd.SoleMountainApp;


/**
 * UI工具类,封装了常用的UI操作
 */
public class UiUtils {

    private static Toast toast;
    private static Toast toastNormal;

    /**
     * 让runnable的run方法中代码到UI线程中执行的方法
     */
    public static void runOnUiThread(Runnable r) {
        SoleMountainApp.getHandler().post(r);
    }

    /**
     * 让runnable的run方法中代码到UI线程中执行的方法
     */
    public static void runOnSubThread(Runnable r) {
        new Thread(r).start();
    }

    /**
     * 在屏幕的中央显示Toast
     */

    public static void showToastCenter(String text) {
        Toast toast = Toast.makeText(SoleMountainApp.getInstance(), text,
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public static int[] getImageWidthHeight(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();

        /**
         * 最关键在此，把options.inJustDecodeBounds = true;
         * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
         */
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回的bitmap为null
        /**
         *options.outHeight为原始图片的高
         */
        return new int[]{options.outWidth, options.outHeight};
    }

    /**
     * 自定义布局Toast
     */

    public static void showToast(Drawable drawable, String text) {
        View view = LayoutInflater.from(SoleMountainApp.getInstance()).inflate(R.layout.mytoast, null);
        TextView textView = (TextView) view.findViewById(R.id.tvToast);
        ImageView imageView = (ImageView) view.findViewById(R.id.ivToast);
        if (drawable == null) {
            imageView.setImageDrawable(UiUtils.getDrawable(R.drawable.gth));
        } else {
            imageView.setImageDrawable(drawable);
        }
        textView.setText(text); // 设置显示文字
        if (toast == null) {
            toast = Toast.makeText(SoleMountainApp.getInstance(), text, Toast.LENGTH_SHORT);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(view);
        toast.show();
    }

    /**
     * 居中Toast
     */

    public static void showToastNormal(String text) {
        if (toastNormal == null) {
            toastNormal = Toast.makeText(SoleMountainApp.getInstance(), text, Toast.LENGTH_SHORT);
        }
        toastNormal.setText(text);
        toastNormal.setGravity(Gravity.CENTER, 0, 0);
        toastNormal.show();
    }

    /**
     * 获取Resources对象
     */
    public static Resources getResources() {
        return SoleMountainApp.getInstance().getResources();
    }

    /**
     * dp转换成dx
     */
    public static int dp2px(float dp) {
        float density = getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5);
    }

    /**
     * 根据id获取vulues/strings.xml中的字符串
     */
    public static String getString(int id) {
        return getResources().getString(id);
    }

    /**
     * 根据id获取vulues/strings.xml中的字符串
     */
    public static float getDimen(int id) {
        return getResources().getDimension(id);
    }

    /**
     * 根据资源id获取res下的图片资源
     */
    public static Drawable getDrawable(int id) {
        return getResources().getDrawable(id);
    }

    /**
     * 根据资源id获取res/values/colors.xml文件中的Color
     */
    public static int getColor(int id) {
        return getResources().getColor(id);
    }

    /**
     * 弹出popupWindow变暗
     */
    public static void turnDark(Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 0.6f;
        context.getWindow().setAttributes(lp);
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    /**
     * 弹出popupWindow变暗, 可自定义暗度
     */
    public static void turnDark(Activity context, float dark) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = dark;
        context.getWindow().setAttributes(lp);
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    /**
     * popupWindow消失的时候恢复
     */
    public static void recover(Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 1.0f;
        context.getWindow().setAttributes(lp);
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    @SuppressWarnings("static-access")
    public static float dp2Px(Context context, float value) {
        if (context == null) {
            return 0;
        }
        TypedValue typedValue = new TypedValue();
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return typedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, metrics);
    }

    @SuppressWarnings("static-access")
    public static float sp2Px(Context context, float value) {
        TypedValue typedValue = new TypedValue();
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return typedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value, metrics);
    }

    public static int dpToPx(float dp, Resources resources) {
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
        return (int) px;
    }

    static public int dipToPx(Context context, int dip) {
        return (int) (dip * getScreenDensity(context) + 0.5f);
    }

    static public float getScreenDensity(Context context) {
        try {
            DisplayMetrics dm = new DisplayMetrics();
            ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                    .getMetrics(dm);
            return dm.density;
        } catch (Exception e) {
            return DisplayMetrics.DENSITY_DEFAULT;
        }
    }

    /**
     * 获得屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
}
